package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityPassAwardsDetailBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterSinglePassProject
import com.gexiaobao.pigeon.ui.dialog.RxDialogDetailInfo
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/7/20 11:17
 * @Describe    : 单关奖项详情
 */
class ActivityPassAwardsDetail : BaseActivity<OrgRaceViewModel, ActivityPassAwardsDetailBinding>() {

    private var projectId = 0
    private val mAdapter: AdapterSinglePassProject by lazy { AdapterSinglePassProject(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        val title = intent.getStringExtra("titleGroup")
        mDatabind.includeBar.tvTitle.text = title
        projectId = intent.getIntExtra("projectId", 0)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
        val headerView = LayoutInflater.from(this).inflate(R.layout.header_pass_awards, null)
        mAdapter.addHeaderView(headerView)

        onRefresh()

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            showDetailInfoDialog(position)
        }
    }

    private fun showDetailInfoDialog(position: Int) {
        val dialog = RxDialogDetailInfo(this)
        dialog.setDta(mAdapter.data[position])
        dialog.setSureListener {
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getAppSignProject(projectId.toString())
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取app单关单只赛的详情  */
        mViewModel.signProjectResult.observe(this) {
            if (mDatabind.includeList.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeList.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.includeList.swipeRecyclerView.isVisible = it.passProjectInfo != null && (it.passProjectInfo as List<*>).isNotEmpty()
            mDatabind.includeEmptyView.llEmptyData.isVisible = it.passProjectInfo == null || (it.passProjectInfo as List<*>).isEmpty()

            mDatabind.data = it.assignInfos
            mAdapter.setList(it.passProjectInfo)
            mAdapter.notifyDataSetChanged()
        }
    }
}